/******************************************************************************
 * Copyright 2022 The Firmament Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/
#include "drv_gpio.h"
#include "hal/pin/pin.h"

#include "device.h"
#include "f28p65x_device.h"

static struct pin_device pin_device;

static void _pin_write(pin_dev_t dev, rt_base_t pin, rt_base_t value)
{
    GPIO_writePin(pin, ((value) ? 1 : 0) );
}

static int _pin_read(pin_dev_t dev, rt_base_t pin)
{
    return GPIO_readPin(pin);
}

static void _pin_mode(pin_dev_t dev, rt_base_t pin, rt_base_t mode, rt_base_t otype)
{
    uint32_t pin_type;
    uint32_t pin_dir;
    uint32_t pin_mux;

    if (otype == PIN_OUT_TYPE_PP) {
        pin_type = GPIO_PIN_TYPE_STD;
    } else if (otype == PIN_OUT_TYPE_OD) {
        pin_type = GPIO_PIN_TYPE_OD;
    }

    switch (mode) {
    case PIN_MODE_OUTPUT:                   /* output setting: not pull */
        pin_type = GPIO_PIN_TYPE_STD;
        pin_dir = GPIO_DIR_MODE_OUT;
        break;
    case PIN_MODE_INPUT:                    /* input setting: not pull. */
        pin_type = GPIO_PIN_TYPE_STD;
        pin_dir = GPIO_DIR_MODE_IN;
        break;
    case PIN_MODE_INPUT_PULLUP:             /* output setting: pull up. */
        pin_type = GPIO_PIN_TYPE_PULLUP;
        pin_dir = GPIO_DIR_MODE_OUT;
        break;
    case PIN_MODE_INPUT_PULLDOWN:           /* output setting: pull down. */
        pin_type = GPIO_PIN_TYPE_STD;
        pin_dir = GPIO_DIR_MODE_OUT;
        break;
    case PIN_MODE_OUTPUT_OD:
        pin_type = GPIO_PIN_TYPE_OD;
        pin_dir = GPIO_DIR_MODE_OUT;
        break;
    default:
        break;
    }

    GPIO_setPadConfig(pin, pin_type);
    GPIO_setDirectionMode(pin, pin_dir);
    GPIO_setQualificationMode(pin, GPIO_QUAL_ASYNC);
    // GPIO_setPinConfig(pin_mux); // TOTO

    // printf("pin:%d, mode:%d, dir:%d, mux:0x%08x\r\n", pin, mode, pin_dir, pin_mux);
}

const static struct pin_ops pin_ops = {
    .pin_mode = _pin_mode,
    .pin_write = _pin_write,
    .pin_read = _pin_read,
};

rt_err_t drv_gpio_init(void)
{
    /* 注意: GPIOxx 默认是否为模拟功能 AIO */
    // GPIO_setAnalogMode(218, GPIO_ANALOG_DISABLED);

    EALLOW;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO203 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO204 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO205 = 0;

    GpioCtrlRegs.GPGAMSEL.bit.GPIO214 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO215 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO216 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO217 = 0;
    GpioCtrlRegs.GPGAMSEL.bit.GPIO218 = 0;
    EDIS;

    pin_device.ops = &pin_ops;

    return hal_pin_register(&pin_device, "pin", RT_DEVICE_FLAG_RDWR, RT_NULL);
}
